package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String,String> template;

    @Override
    public String login(UcenterMember member) {
        //判断手机号或密码是否存在，一个不存在就抛出异常
        if(StringUtils.isEmpty(member.getMobile()) || StringUtils.isEmpty(member.getPassword())){
            throw new GuliException(20001,"手机号或密码未输入");
        }
        //根据手机号查询用户
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",member.getMobile());
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember == null){
            throw new GuliException(20001,"此用户不存在");
        }
        //验证密码是否正确,密码需通过MD5加密再判断
        if(!MD5.encrypt(member.getPassword()).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登录名或密码错误");
        }
        //判断该账户是否被禁用
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"账户已被禁用");
        }
        //验证通过，生成token
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo vo) {
        String nickname = vo.getNickname();
        String mobile = vo.getMobile();
        String code = vo.getCode();
        String password = vo.getPassword();
        //校验注册信息合法性
        if(StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile) ||
        StringUtils.isEmpty(code) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"信息不全");
        }
        //验证输入的验证码是否有效
        String redisCode = template.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }
        //校验该手机是否被注册过
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"该手机号已被注册");
        }
        UcenterMember member=new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setIsDisabled(false);
        //密码先MD5加密
        member.setPassword(MD5.encrypt(password));
        //默认头像
        member.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/65423f14-49a9-4092-baf5-6d0ef9686a85.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        Integer count=baseMapper.countRegister(day);
        return count;
    }
}
